﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Design;

namespace MethodWorx.CMS.TemplateLanguage.Parsers
{
    public class StringSplit : IParser, IDesignable
    {
        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            object s = stack.FirstOrDefault();
            if (!(s is string))
                throw new Exception("Value to parse is not a string");
            else
            {

                List<string> ret = new List<string>();
                foreach (var st in ((string)s).Split('\n'))
                {
                    var trimmed = st.Trim();
                    if (!string.IsNullOrEmpty(trimmed))
                        ret.Add(trimmed);
                }
                return ret;
            }
        }

       
        public string Name
        {
            get { return "split"; }
        }

        public string Namespace
        {
            get { return "string"; }
        }

        public string[] Parameters
        {
            get { return new string[] { }; }
        }

        public void Design(IDesigner designer)
        {
            designer.AddControl("String:Split")
                .SetControlType(DesignerControlType.Label)
                .AddContentBeforeChildren("Split Values Into:");
        }
    }
}
